﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Net.Mail;

namespace VK.Comm
{
    public static class MailHandle
    {
        public static bool ResetPassSend(string toEmail, string fromName, string subject, string toContent)
        {
            bool result = false;

            string mailServerEmail = ConfigurationManager.AppSettings["MailServerEmail"];
            string mailServerName = ConfigurationManager.AppSettings["MailServerName"];
            string mailServerPass = ConfigurationManager.AppSettings["MailServerPass"];
            string mailServerSmtp = ConfigurationManager.AppSettings["MailServerSmtp"];

            MailMessage mail = new MailMessage();

            mail.From = new MailAddress(mailServerEmail, fromName);  //发件人
            mail.To.Add(toEmail);                  //收件人
            mail.Subject = subject;         //主题
            mail.Body = toContent;                 //内容

            //邮件主题和正文的编码格式
            mail.SubjectEncoding = System.Text.Encoding.UTF8;
            mail.BodyEncoding = System.Text.Encoding.UTF8;

            mail.IsBodyHtml = false;                //邮件正文允许html编码
            mail.Priority = MailPriority.Normal;    //优先级

            SmtpClient client = new SmtpClient();
            client.Host = mailServerSmtp;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.Credentials = new System.Net.NetworkCredential(mailServerName, mailServerPass);
            //client.EnableSsl = true;//SMTP 服务器要求安全连接需要设置此属性
            try
            {
                client.Send(mail);
                result = true;
            }
            catch { }

            return result;
        }
    }
}
